<?php

$g_tplfiles = array();

/**
	@brief Outputs a templated page
	
	@brief $page_template Template filename
 */
function render($page_template)
{
	global $g_tplvars;
	$g_tplvars['body'] = templatize($page_template);
	echo templatize('../templates/template.html');
}

/**
	@brief Applies template transformations to a file
 */
function templatize($fname)
{
	//Cache file contents
	global $g_tplfiles;
	if(!isset($g_tplfiles[$fname]))
		$g_tplfiles[$fname] = file_get_contents($fname);
	
	//and do the fun stuff
	return dotemplatize($g_tplfiles[$fname]);
}

/**
	@brief Applies template transformations to a string
 */
function dotemplatize($str)
{
	global $g_tplvars;
	foreach($g_tplvars as $name => $val)
		$str = str_replace("<!-- [[$name]] -->", $val, $str);
	return $str;
}
?>
